package com.xxyuan.batch._37_step_part;

import org.springframework.batch.core.partition.support.Partitioner;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户文件读取分区器
 * 指定从步骤需要的上下文环境
 */
public class UserPartitioner implements Partitioner {

    int begin = 1;
    int end = 10;
    int range = 10;
    String text = "user%s-%s.txt";
    //map:key:从步骤名称 value：从步骤上下文环境 gridSize：从步骤个数

    @Override
    public Map<String, ExecutionContext> partition(int gridSize) {
        Map<String, ExecutionContext> map = new HashMap<>(gridSize);

        for (int i = 0; i < gridSize; i++) {
            ExecutionContext ex = new ExecutionContext();
            //String name = "user" + begin + "-" + end +".txt";
            String name = String.format(text, begin, end);
            Resource resource = new ClassPathResource(name);

            //springbatch转换，单纯使用对象时会报错，可以使用url字符串形式，srping会自动加载
            try {
                ex.putString("file",resource.getURL().toExternalForm());
            } catch (IOException e) {
                e.printStackTrace();
            }
            begin += range;
            end += range;
            map.put("user_partition_"+i,ex);
        }
        return map;
    }
}
